<template>
  <view class="body">
    <button class="button" hover-class="hover" @tap="labelTest" :loading="isLabelSend"
      :disabled="isLabelSend">标签打印</button>
  </view>
</template>

<script>
var app = getApp();
var tsc = require("../../../utils/tsc.js");
export default {
  data() {
    return {
      looptime: 0,
      currentTime: 1,
      lastData: 0,
      oneTimeData: 0,
      buffSize: [],
      //发送字节数下标
      printNum: [],
      printerNum: 1,
      currentPrint: 1,
      isLabelSend: false,
      getAssets: {}

    };
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var list = [];
    var numList = [];
    var j = 0;

    for (var i = 20; i < 200; i += 10) {
      list[j] = i;
      j++;
    }

    for (var i = 1; i < 10; i++) {
      numList[i - 1] = i;
    }

    this.setData({
      buffSize: list,
      oneTimeData: list[0],
      printNum: numList,
      printerNum: numList[0]
    });
  },


  onShow() {
    this.getAssets = uni.getStorageSync('Assets')

  },

  methods: {
    labelTest: function () {
      //标签测试
      var that = this;
      var command = tsc.jpPrinter.createNew();
      command.setCls(); //清除缓冲区，防止下一个没生效
      command.setSize(50, 30); //设置标签大小，单位mm.具体参数请用尺子量一下
      command.setGap(0); //设置两个标签之间的间隙，单位mm.具体参数请用尺子量一下
      command.setCls(); //清除缓冲区
      if (that.getAssets.assetsName.length >= 5) {
        that.getAssets.assetsName = that.getAssets.assetsName.slice(0, 5) + '...'
      }
      if (that.getAssets.depository.length >= 5) {
        this.getAssets.depository = that.getAssets.depository.slice(0, 5) + '...'
      }
      if (that.getAssets.source.length >= 5) {
        that.getAssets.source = that.getAssets.source.slice(0, 5) + '...'
      }
      if (that.getAssets.deposit.length >= 5) {
        that.getAssets.deposit = that.getAssets.deposit.slice(0, 5) + '...'
      }
      command.setText(40, 10, "TSS14.BF2", 0, 1, 1, "长江三峡通航管理局")
      command.setText(0, 60, "TSS14.BF2", 0, 1, 1, `资产名称:${that.getAssets.assetsName}`); //绘制文字
      command.setText(260, 40, "TSS14.BF2", 0, 1, 1, `${that.getAssets.assetsNo}`); //绘制文字
      command.setText(0, 90, "TSS14.BF2", 0, 1, 1, `登账日期:${that.getAssets.registerDate}`); //绘制文字
      command.setText(0, 120, "TSS14.BF2", 0, 1, 1, `使用部门:${that.getAssets.depository}`); //绘制文字
      command.setText(0, 150, "TSS14.BF2", 0, 1, 1, `资产来源:${that.getAssets.source}`); //绘制文字
      command.setText(0, 180, "TSS14.BF2", 0, 1, 1, `存放地点:${that.getAssets.deposit}`); //绘制文字
      var url = "http://yxadmin.xyz:8080/weixin?id=" + that.getAssets.assetsNo
      command.setQR("240,70,L,1,M,0,M1,S1,\"" + url + "\"\r\n")
      command.setPagePrint(); //执行打印指令
      that.setData({
        isLabelSend: true
      });
      that.prepareSend(command.getData());
    },
    prepareSend: function (buff) {
      //准备发送，根据每次发送字节数来处理分包数量
      //console.log(buff)
      var that = this;
      var time = that.oneTimeData;
      var looptime = parseInt(buff.length / time);
      var lastData = parseInt(buff.length % time); //console.log(looptime + "---" + lastData)

      that.setData({
        looptime: looptime + 1,
        lastData: lastData,
        currentTime: 1
      });
      that.Send(buff);
    },

    Send: function (buff) {
      //分包发送
      var that = this;
      var currentTime = that.currentTime;
      var loopTime = that.looptime;
      var lastData = that.lastData;
      var onTimeData = that.oneTimeData;
      var printNum = that.printerNum;
      var currentPrint = that.currentPrint;
      var buf;
      var dataView;

      if (currentTime < loopTime) {
        buf = new ArrayBuffer(onTimeData);
        dataView = new DataView(buf);

        for (var i = 0; i < onTimeData; ++i) {
          dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i]);
        }
      } else {
        buf = new ArrayBuffer(lastData);
        dataView = new DataView(buf);

        for (var i = 0; i < lastData; ++i) {
          dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i]);
        }
      } //console.log("第" + currentTime + "次发送数据大小为：" + buf.byteLength)


      uni.writeBLECharacteristicValue({
        deviceId: app.globalData.BLEInformation.deviceId,
        serviceId: app.globalData.BLEInformation.writeServiceId,
        characteristicId: app.globalData.BLEInformation.writeCharaterId,
        value: buf,
        success: function (res) {
          if (currentPrint == printNum) {
            uni.showToast({
              title: '已打印第' + currentPrint + '张成功'
            });
          } //console.log(res)

        },
        fail: function (e) {
          uni.showToast({
            title: '打印第' + currentPrint + '张失败',
            icon: 'none'
          }); //console.log(e)
        },
        complete: function () {
          currentTime++;

          if (currentTime <= loopTime) {
            that.setData({
              currentTime: currentTime
            });
            that.Send(buff);
          } else {
            wx.showToast({
              title: '已打印第' + currentPrint + '张',
            })
            if (currentPrint == printNum) {
              that.setData({
                looptime: 0,
                lastData: 0,
                currentTime: 1,
                isLabelSend: false,
                currentPrint: 1
              });
            } else {
              currentPrint++;
              that.setData({
                currentPrint: currentPrint,
                currentTime: 1
              });
              that.Send(buff);
            }
          }
        }
      });
    },


  }
};
</script>
<style>
.button {
  margin-top: 20px;
  width: 90%;
  background-color: #54bec2;
  color: white;
  border-radius: 98rpx;
  background: bg_red;
}

/* 按下变颜色 */
.hover {
  background: #dcdcdc;
}
</style>
